package com.sky.demo2;

/**
 * @author : wushikai
 * <p>
 * date : 2020-11-28
 */

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;


public class AnnotationUtil {

    /**
     * 获取打了Desc注解的字典属性列表
     * @return 字典属性列表
     */
    public static <T> List<FixedVo> getFixedVoList(Class<T> clazz) {
        if (clazz == null) {
            return Collections.emptyList();
        }
        try {
            T instance = clazz.getDeclaredConstructor().newInstance();  //获取泛型对象
            Field[] fields = clazz.getDeclaredFields();  //获取泛型类的字段
            List<FixedVo> fixedVoList = new LinkedList<FixedVo>();

            for (Field field : fields) {
                Description desc = field.getAnnotation(Description.class);
                if (desc != null) {
                    FixedVo vo = new FixedVo();
                    vo.setKey(field.getInt(instance));
                    vo.setValue(desc.value());
                    vo.setRemark(desc.remark());
                    fixedVoList.add(vo);
                }
            }

            Mapper annotation = clazz.getAnnotation(Mapper.class);
            if (annotation !=null){

                System.out.println( clazz.getCanonicalName()+ "这个类已经被mybatis 标记上了....   ");
            }

            Method[] declaredMethods = clazz.getDeclaredMethods();
            for (Method method: declaredMethods){


                boolean annotationPresent = method.isAnnotationPresent(Select.class); //判断这个方法是否有 @select 注解
                if (annotationPresent){
                    Select select = method.getAnnotation(Select.class);
                    System.out.println( method.getName()+   //获取注解的值, 即SQL 语句;
                            "  正在执行查询语句....  SQL ====> "+ Arrays.toString(select.value()) );
                }

            }


            return fixedVoList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }


    public static void main(String[] args){


        List<FixedVo> voList = AnnotationUtil.getFixedVoList(UserStatus.class);
        for (FixedVo vo : voList) {
            System.out.println(vo.toString());
        }



    }



}